package com.company.thread;

/**
 * @description: 使用DCL（double-check-lock）创建单例模式, 使用 volatile 禁止指令重排
 * @author: ruoan
 * @date: 2020/12/8 13:13
 */
public class SingletonDemo3 {
    private static volatile SingletonDemo3 instance;

    private SingletonDemo3() {
        System.out.println(Thread.currentThread().getName()+"在构造器");
    }

    public static SingletonDemo3 getInstance(){
        if (instance == null) {
            synchronized (SingletonDemo3.class) {
                if (instance == null) {
                    instance = new SingletonDemo3();
                }
            }
        }
        return instance;
    }


    public static void main(String[] args) {

        SingletonDemo3 singletonDemoInMain = SingletonDemo3.getInstance();
        SingletonDemo3 singletonDemoInMain2 = SingletonDemo3.getInstance();
        System.out.println("hh");
        System.out.println(singletonDemoInMain==singletonDemoInMain2);




    }
}
